#!/usr/bin/env python3
#coding: utf-8
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage

sender = 'sudohuoty@163.com'
receiver = '1346632121@qq.com'
subject = 'python email test'
smtpserver = 'smtp.163.com'
username = 'sudohuoty'
password = 'xxxxxxx'

msgRoot = MIMEMultipart('related')
msgRoot['Subject'] = 'test message'
msgRoot['To'] = '1346632121@qq.com'
msgRoot['From'] = 'sudohuoty@163.com'

msgText = MIMEText('''<b>Some <i>HTML</i> text</b> and an image.
<img alt="" src="cid:0" />
good!''','html','utf-8')
msgRoot.attach(msgText)

fp = open('/home/konghy/temp/psb.jpg', 'rb')
msgImage = MIMEImage(fp.read())
fp.close()

msgImage.add_header('Content-Disposition', 'attachment', filename='test.png')
msgImage.add_header('Content-ID', '<0>')
msgImage.add_header('X-Attachment-Id', '0')
msgRoot.attach(msgImage)

att2 = MIMEText(open('/home/konghy/temp/test.pdf', 'rb').read(), 'base64', 'gb2312')
att2["Content-Type"] = 'application/octet-stream'
att2["Content-Disposition"] = 'attachment; filename="test.pdf"'
msgRoot.attach(att2)


smtp = smtplib.SMTP()
smtp.connect('smtp.163.com')
smtp.login(username, password)
smtp.sendmail(sender, receiver, msgRoot.as_string())
smtp.quit()


